package fm.last.android;

import java.util.Locale;

import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;

public class AccountDemo extends Application {
	
	private static AccountDemo mInstance = null;
	
	public static AccountDemo getInstance() {
		if(mInstance == null) {
			mInstance = new AccountDemo();
		}
		return mInstance;
	}
	
	public Session session;

	@Override
    public void onCreate() {
		super.onCreate();
		mInstance = this;
		
		String version;
		try {
			version = "/" + AccountDemo.getInstance().getPackageManager().getPackageInfo("fm.last.android", 0).versionName;
		} catch (Exception e) {
			version = "";
		}
		
		UrlUtil.useragent = "MobileLastFM" + version + " (" + android.os.Build.MODEL + "; " + Locale.getDefault().getCountry().toLowerCase() + "; "
				+ "Android " + android.os.Build.VERSION.RELEASE + ")";

		// Populate our Session object
		SharedPreferences settings = getSharedPreferences(LastFm.PREFS, 0);
		String username = settings.getString("lastfm_user", "");
		String session_key = settings.getString("lastfm_session_key", "");
		String subscriber = settings.getString("lastfm_subscriber", "0");
		
		session = new Session(username, session_key, subscriber);
		
		version = "0.1";
		try {
			version = getPackageManager().getPackageInfo("fm.last.android", 0).versionName;
		} catch (NameNotFoundException e) {
		}
    }

	@Override
    public void onTerminate() {
		session = null;
		mInstance = null;
		super.onTerminate();
    }
	
	public void logout() {
		SharedPreferences settings = getSharedPreferences(LastFm.PREFS, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove("lastfm_user");
		editor.remove("lastfm_pass");
		editor.remove("lastfm_session_key");
		editor.remove("lastfm_subscriber");
		editor.remove("scrobbler_session");
		editor.remove("scrobbler_subsurl");
		editor.remove("scrobbler_npurl");
		editor.commit();
		session = null;
	}
	
	public void presentError(Context ctx, WSError error) {
		int title = R.string.ERROR_SERVER_UNAVAILABLE_TITLE;
		int description = R.string.ERROR_SERVER_UNAVAILABLE;

		if(error != null) {
			switch (error.getCode()) {
			case WSError.ERROR_AuthenticationFailed:
			case WSError.ERROR_InvalidSession:
				title = R.string.ERROR_SESSION_TITLE;
				description = R.string.ERROR_SESSION;
				break;
			case WSError.ERROR_InvalidAPIKey:
				title = R.string.ERROR_UPGRADE_TITLE;
				description = R.string.ERROR_UPGRADE;
				break;
			case WSError.ERROR_SubscribersOnly:
				title = R.string.ERROR_SUBSCRIPTION_TITLE;
				description = R.string.ERROR_SUBSCRIPTION;
				break;
			default:
				title = R.string.ERROR_SERVER_UNAVAILABLE_TITLE;
				description = R.string.ERROR_SERVER_UNAVAILABLE;
			break;
			}
		}

		presentError(ctx, getResources().getString(title), getResources().getString(description));
	}
	
	public void presentError(Context ctx, String title, String description) {
		AlertDialog.Builder d = new AlertDialog.Builder(ctx);
		d.setTitle(title);
		d.setMessage(description);
		d.setIcon(android.R.drawable.ic_dialog_alert);
		d.setNeutralButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		try {
			d.show();
		} catch (Exception e) {}
	}
}
